/**
 * 
 */
package com.sms.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.sms.businessservice.UserBusinessService;
import com.sms.validator.UserValidator;
import com.sms.vo.UserVO;

/**
 * @author Sahdev Kamboj
 * 
 */
public class UserController extends MultiActionController {
	private static final Logger LOG = Logger.getLogger(UserController.class);

	private BindingResult errors = null;
	
	private UserBusinessService userBusinessService;
	
	private UserValidator userValidator;

	/**
	 * @param userBusinessService
	 *            the userBusinessService to set
	 */
	public void setUserBusinessService(UserBusinessService userBusinessService) {
		LOG.debug("setUserBusinessService(UserBusinessService userBusinessService) : " + userBusinessService);
		this.userBusinessService = userBusinessService;
	}

	@Override
	protected void bind(HttpServletRequest request, Object command) throws Exception {
		LOG.debug("bind(HttpServletRequest request, Object command) throws Exception : " + request + "[ Object : " + command);
		final ServletRequestDataBinder binder = new ServletRequestDataBinder(command);
		binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
		binder.bind(request);
		errors = binder.getBindingResult();
		ValidationUtils.invokeValidator(userValidator, command, errors);

	}

	public ModelAndView showUserUI(HttpServletRequest request, HttpServletResponse response) throws Exception {
		final Map<String, Object> model = new HashMap<String, Object>();
		UserVO userVO = new UserVO();
		userVO.setUserName("Amit Kamboj");
		model.put("userVO", userVO);
		return new ModelAndView("User", model);
	}

	public ModelAndView saveUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		final Map<String, Object> model = new HashMap<String, Object>();
		UserVO userVO = new UserVO();
		bind(request, userVO);
		String login = request.getParameter("Login");
		if (login != null) {
			if (errors == null || errors.getAllErrors() == null || errors.getAllErrors().size() == 0) {
				userBusinessService.saveUser(userVO);
				model.put("infoMsg", "User.list.updated");
			} else {
				model.put("error", errors);

			}
			model.put("userVO", userVO);
		}
		return new ModelAndView("User", model);

	}

	/**
	 * @param userValidator
	 *            the userValidator to set
	 */
	public void setUserValidator(UserValidator userValidator) {
		this.userValidator = userValidator;
	}

	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		
		return super.handleRequest(request, response);
	}
	
}
